# -*- coding:utf-8 -*-
from fastapi import Request
from app.entity.system import SysClient
from core.curd import CRUDBase, IBaseDao
from sqlalchemy.ext.asyncio import AsyncEngine
from sqlalchemy import select, text


class SysClientDao(IBaseDao):
    """
    测试模块服务层
    """

    def __init__(self, req: Request):
        self.db_session: AsyncEngine = req.app.state.db_async_session
        self.base = CRUDBase(SysClient)

    async def get_list(self):
        async with self.db_session() as db:
            query = (select(SysClient.name, SysClient.path, SysClient.appid, SysClient.version).filter(SysClient.status==1).order_by(SysClient.sort)).limit(100)

            rs = await db.execute(query)

            rows = map(lambda x: {"name": x[0], "path": x[1], "client_id": x[2], "version": x[3]}, rs.fetchall())
            return rows

    @classmethod
    async def instance(cls, req: Request):
        return cls(req)
